package com.heima.shopweb.controller;

import com.heima.shopweb.service.MemberFeignService;
import com.zijie.core.util.JsonResult;
import com.zijie.members.model.Members;
import com.zijie.members.service.MemberService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/shopping/cart/settle")
public class SettleController {
    private MemberFeignService memberFeignService;

    @Autowired
    public void setMemberFeignService(MemberFeignService memberFeignService) {
        this.memberFeignService = memberFeignService;
    }

    @PostMapping("/update")
    @ResponseBody
    public JsonResult<Members> updateAddress(
            @RequestParam(name = "id", required = false) Integer id,
            @RequestParam(name = "address", required = false) String address,
            HttpSession session) {
        Members members=new Members();
//        Integer memberId = (Integer) session.getAttribute("memberId");
        members.setId( id);
        members.setAddressDetail(address);
        try {
            ResponseEntity<JsonResult<Members>> update = memberFeignService.update(members);
            JsonResult<Members> result = update.getBody();
            
            // 检查返回结果是否有效
            if (result != null && "200".equals(result.getCode())) {
                return result;
            } else if (result != null) {
                // 返回错误信息，但data字段设为null避免类型不匹配
                return new JsonResult<>(result.getCode(), result.getMsg(), null);
            } else {
                return new JsonResult<>("500", "更新失败", null);
            }
        } catch (Exception e) {
            // 捕获Feign调用异常
            return new JsonResult<>("500", "更新失败: " + e.getMessage(), null);
        }
    }
}